package com.example.myblog1_0.service.Impl;

import com.example.myblog1_0.model.MyUser;
import com.example.myblog1_0.mapper.UserMapper;
import com.example.myblog1_0.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: xc
 * @Date: 2018/12/14 16:17
 * @Description:
 **/
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    UserMapper userMapper;
    @Override
    public MyUser findUserInfo(MyUser myUser) {
        return userMapper.findUserInfo(myUser);
    }

    @Override
    public boolean insetUser(MyUser myUser) {
        return userMapper.insetUser(myUser);
    }
    @Override
    public MyUser findUserInfoByName(String username){
        return userMapper.findUserInfoByName(username);
    }

    @Override
    public MyUser findUserInfoByOpenid(String openid) {
        return userMapper.findUserInfoByOpenid(openid);
    }

    @Override
    public boolean updateQqUser(MyUser myUser) {
        return userMapper.updateQqUser(myUser);
    }
    @Override
    public JSONObject getUserInfoByUsername(String username){
        JSONObject jsonObject = new JSONObject();
        MyUser findUser = userMapper.findUserInfoByName(username);
        jsonObject.put("username",findUser.getUsername());
        jsonObject.put("nickname",findUser.getNickname());
        jsonObject.put("avatarUrl",findUser.getAvatarUrl());
        jsonObject.put("phone",findUser.getPhone());
        return jsonObject;
    }

    @Override
    public JSONObject updateNickname(String username,String nickname) {
        JSONObject jsonObject = new JSONObject();
        if (userMapper.findUserByNickname(nickname)!=null){
            jsonObject.put("status",500);
            jsonObject.put("message","昵称已存在");
            return jsonObject;
        }
        if(userMapper.updateNickname(username,nickname)){
            jsonObject.put("status",200);
            jsonObject.put("message","更新成功");
        }else {
            jsonObject.put("status",0);
            jsonObject.put("message","更新失败");
        }
        return jsonObject;
    }

    @Override
    public JSONObject updateAvatar(String username, String avatarUrl) {
        JSONObject jsonObject  = new JSONObject();
        if (userMapper.updateAvatar(username,avatarUrl)){
            jsonObject.put("status",200);
            jsonObject.put("message","更新成功");
            return jsonObject;
        }else {
            jsonObject.put("status",0);
            jsonObject.put("message","更新失败");
            return jsonObject;
        }

    }

    @Override
    public JSONObject findUserByNickname(String nickname) {
        JSONObject jsonObject = new JSONObject();
        if (userMapper.findUserByNickname(nickname)!=null){
            jsonObject.put("status",500);
            jsonObject.put("message","昵称已存在");
            return jsonObject;
        }else {
            jsonObject.put("status",200);
            jsonObject.put("message","昵称可用");
            return jsonObject;
        }
    }

    @Override
    public MyUser getUserByNickname(String nickname) {
        return userMapper.findUserByNickname(nickname);
    }

}
